#include "includes.h"


void butterfly_welcome()
{
	butterfly_print("Welcome!");
	_delay_ms(2000);
}


void butterfly_menu(void)
{
	enum menu_state_enum menu_state = UNDER;
	char button = '0',last_button='0';
	
	butterfly_print("Select mode");
	_delay_ms(4000);

	while(button=='0')
	{
		button = butterfly_poll_joystick();
	}

	while(1) 
	{
		last_button = button;
		button = butterfly_poll_joystick();
		if(button != last_button) 
		{
            switch (button) 
			{
                case KEY_ENTER: game_start(menu_state); break;
	            case KEY_NEXT: continue;
                case KEY_PREV: continue;
                case KEY_PLUS: menu_state++; break;
                case KEY_MINUS:menu_state--; break;
                default: break;
            }
			if (menu_state >= OVER) menu_state = UNDER+1;
			if (menu_state <= UNDER) menu_state = OVER-1;
            
			switch (menu_state)
			{
				case SINGLEPLAYERPOINTS:  butterfly_print("1P with points"); break;
				case SINGLEPLAYERETERNAL: butterfly_print("1P eternally"); break;
				case MULTIPLAYERPOINTS:   butterfly_print("2P with points"); break;
				case MULTIPLAYERETERNAL:  butterfly_print("2P eternally"); break;
				default: break;
			}	
		}
	}
}


void butterfly_test_joystick(void) 
{
	char button = '0',last_button;
    while(TRUE) 
	{
		last_button = button;
		button = butterfly_poll_joystick();
		if(button != last_button) 
		{
            switch (button) 
			{
                case KEY_ENTER:
                    SPI_MasterSend("ENTER");
                    break;
                case KEY_NEXT:
                    SPI_MasterSend("RIGHT");
                    break;
                case KEY_PREV:
                    SPI_MasterSend("LEFT");
                    break;
                case KEY_PLUS:
                    SPI_MasterSend("UP");
                    break;
                case KEY_MINUS:
                    SPI_MasterSend("DOWN");
                    break;
                default:
                   // SPI_MasterSend("TEIT");
                    break;
            }
		}
	}
}


char butterfly_poll_joystick(void)
{
	cli();
	char button;
	enable_spi_transmission(BUTTERFLY);
	SPI_write(0x03);
	_delay_ms(20);
	SPI_write(0x04);
	_delay_ms(20);
	button = SPDR;
	disable_spi_transmission(BUTTERFLY);
	sei();
	return button;  	
}


void butterfly_print(char string[28])
{
	cli();
	int i=0;
	enable_spi_transmission(BUTTERFLY);
	_delay_ms(20);
	SPI_write(0x01);
	while(string[i]!='\0') {
		SPI_write(string[i]);
		i++;
	}
	SPI_write(string[i]);
	SPI_write(0x02);
	disable_spi_transmission(BUTTERFLY);
	_delay_ms(20);
	sei();
}


void butterfly_print_noscroll(char string[28])
{
	cli();
	int i=0;
	enable_spi_transmission(BUTTERFLY);
	_delay_ms(20);
	SPI_write(0x01);
	while(string[i]!='\0') {
		SPI_write(string[i]);
		i++;
	}
	SPI_write(string[i]);
	SPI_write(0x05);
	disable_spi_transmission(BUTTERFLY);
	_delay_ms(20);
	sei();
}


